if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {

	var user = {};
	var html = [];
//	var orderid = getUrlParam('orderid');
	var mainorderno = getUrlParam('mainorderno');
	var thisPage = {};
	var payFailPage = function() {
		this.init();
	}
	payFailPage.prototype = {
		init: function() {
			this.initData();
			this.bindEvent();
		},
		initData: function() {
			var thisModel = this;
			user = getLocalStorage(STORAGE_MANAGER.user);
			if (!user) {
				redirect("../account/login.html");
				return;
			}
      if(!mainorderno){
      	$.alert('没有找到订单号', '', '', function(){
      		back();
      	});
      }
			$.ajax({
				type: "GET",
				headers: {token: user.token},
				url: CONFIG_MANAGER.RequstUrl + "v1/orders/main/"+ mainorderno +"/" + user.id,
				async: true,
				contentType: 'application/json',
			}).done(function(data){
				thisModel.initPage(data);
			}).fail(function(data){ 
				$.closeWaiting();
				$.toast(data.responseJSON.Message);				
			}).always(function(data){
				$.closeWaiting();
			})
		},
		//初始化页面
		initPage:function(data){
			html.push('<div class="zcchengg">');
			html.push('<p class="zccgdui"><img src="../../images/zccgdui1.png" /></p>');
			html.push('<p class="gongxi"><span>没有支付成功，请重新支付</span>');
			html.push('<br />订单将在20分钟内自动关闭</p>');
			html.push('</div>');
			
			var i = 0;
			var j = 0;
			var total_qty = 0;
			var total_coin = 0;	
			while (data[i]){
				var order = data[i];
				html.push('<div class="wlxqbg">');
				html.push('<div class="wlxqnrbg">');
				html.push('<div class="bg-fff mbtopbottom ddnybg">');
				html.push('<div class="ddnybbt mbbottom">');
				html.push('<p class="shxxbt1"><span>'+ (order.shop_name || '') +'</span></p>');
				html.push('</div>');
				var items = order.items;
				while (items[i]){
					html.push('<div class="mbbottom  overflow">');
					html.push('<dl class="cpxqxztop  mtop0">');
					html.push('<dt><img src="'+ getImageUrl(items[i].logo,65,61) +'" /></dt>');
					html.push('<dd class="wdgzwz wdgzwz-20">');
					html.push('<p class="floatr  money2"><span class="jinbi"><b>');
					html.push('<span class="jinbi"></span><span class="money">&yen;'+ items[i].act_price +'+</span><img src="../../images/jbtub.jpg"></b>'+ items[i].coin +'</span></p>' + items[i].sku_name);
					html.push('</dd>');
					html.push('<dd class="huisezi"><span class="x2">x'+ items[i].qty +'</span>'+ items[i].specs +'</dd>');
					html.push('</dl>');
					html.push('</div>');
					i++;
				}				
				html.push('<div class="wlxqnrbg">');
				html.push('<p class="wlxqnr mbbottom"><span>'+ order.pay_typetext + ' ' + order.delivery_type_text +'</span>支付配送</p>');
				j++;
			}
			html.push('<p class="wlxqnr mbbottom"><span>'+ (order.invoice_title||'') +'</span>开具发票</p>');
			html.push('</div>');
			html.push('<div class="gjjsl mbbottom">');
			html.push('<p class="floatr">共计'+ order.ttl_count +'件商品合计');
			html.push('<span class="jinbi"></span><span class="money">&yen;'+ (order.pay_fee) +'</span>+<span class="jinbi"><b><img src="../../images/jbtub.jpg"></b>'+ total_coin +'</span>');
			html.push('</p>');
			html.push('</div>');
			html.push('</div>');
			html.push('</div>');
			html.push('</div>');
			html.push('<p id="returnPay" class="sjqyan">重新支付</p>');
			html.push('<p id="returnIndex" class="sjbban">返回首页</p>');	
			$("#main").html(html.join(''));
		},
		bindEvent: function() {
			//回退
			$("#back").on("click", function() {
//				if (AGENT_CLIENT.APP) {
//					plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
//					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");					
//				} else{
//					redirect("../home/index.html");
//				}
				back();
			});
			
			//重新支付
			$("#main").on("click", "#returnPay", function(){
				redirect("payCenter.html?mainorderno=" + mainorderno);
			});
			
			//返回首页
			$("#main").on("click", "#returnIndex", function(){ 
				if(AGENT_CLIENT.APP){
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
				}else{
					redirect("../home/index.html");
				}
			});
		}
	};

	thisPage = new payFailPage();
}